﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;

namespace VideoRentalShop.View.Templates
{
    /// <summary>
    /// Logika interakcji dla klasy ListPanel.xaml
    /// </summary>
    public partial class ListPanel : UserControl
    {
        private static readonly GridLength NO_SIZE = new GridLength(0d, GridUnitType.Pixel);


        public ListPanel()
        {
            InitializeComponent();
            colEdit.Width = NO_SIZE;
            rowAdditional.Height = NO_SIZE;

            base.IsVisibleChanged += new DependencyPropertyChangedEventHandler((o, e) =>
            {
                lstResults.ItemsSource = null;
                Refresh();
            });
        }


        protected virtual void Refresh() { }

        protected virtual void RemoveClicked() { }
        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            var count = lstResults.SelectedItems.Count;

            if (count == 0)
            {
                MessageBox.Show("Wymagane jest uprzednie wybranie elementów do usunięcia.", "Zaznacz elementy");
                return;
            }

            var prefix = string.Empty;
            if (count > 4) { prefix = "ów"; }
            else if (count > 1)  { prefix = "y"; }

            var result = MessageBox.Show(string.Format("Czy na pewno chcesz usunąć {0} element{1}?", count, prefix), "Usuwanie elementów", MessageBoxButton.YesNo, MessageBoxImage.Warning);

            if (result == MessageBoxResult.Yes)
            {
                RemoveClicked();
            }
        }

        protected virtual void EditClicked() { }
        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            var count = lstResults.SelectedItems.Count;

            if (count == 0)
            {
                MessageBox.Show("Wymagane jest uprzednie wybranie elementu z listy.");
                return;
            }
            else if (count > 1)
            {
                var result = MessageBox.Show("Edytowany zostanie tylko element wybrany jako pierwszy. Czy chcesz kontynuować?", "Edycja", MessageBoxButton.YesNo, MessageBoxImage.Question);

                if (result != MessageBoxResult.Yes)
                {
                    return;
                }
            }

            EditClicked();
        }

        protected virtual void AddClicked() { }
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            AddClicked();
        }

        protected virtual void SearchClicked() { }
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            SearchClicked();
        }

        protected virtual void ClearClicked() { }
        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            ClearClicked();
        }


        protected void AddSearchPanel(FrameworkElement panel)
        {
            panel.Visibility = Visibility.Visible;
            panel.Width = gridSearch.Width;
            panel.Height = gridSearch.Height;
            gridSearch.Children.Add(panel);
        }

        protected void AddEditPanel(FrameworkElement panel)
        {
            panel.Visibility = Visibility.Visible;
            panel.Width = gridSearch.Width;
            panel.Height = gridSearch.Height;
            gridEdit.Children.Add(panel);

            colEdit.Width = colSearch.Width;
            colEdit.MinWidth = colSearch.MinWidth;
        }

        protected void AddAdditionalPanel(FrameworkElement panel, GridLength height)
        {
            panel.Visibility = Visibility.Visible;
            panel.Width = gridSearch.Width;
            panel.Height = gridSearch.Height;
            gridAdditional.Children.Add(panel);
            rowAdditional.Height = height;
        }

        protected void AddListColumn(string name, string bindName)
        {
            lstResultsGrid.Columns.Add(new GridViewColumn { Header = name, DisplayMemberBinding = new Binding(bindName) });
        }

        protected void AddListColumn(string name, BindingBase binding)
        {
            lstResultsGrid.Columns.Add(new GridViewColumn { Header = name, DisplayMemberBinding = binding });
        }

        protected void ShowListResults(IEnumerable results)
        {
            lstResults.ItemsSource = results;

            if (lstResults.Items.Count == 0)
            {
                MessageBox.Show("Brak wyników dopasowanych do wybranych opcji", "Brak danych", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        protected virtual void SelectionChanged() { }
        private void lstResults_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectionChanged();
        }
    }
}
